﻿using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Songhay.OpenXml.Tests
{
    using Songhay.Composition;
    using Songhay.OpenXml;
    using Songhay.OpenXml.Models;

    /// <summary>
    /// Tests for <see cref="Songhay.OpenXml.OpenXmlUtility"/>.
    /// </summary>
    [TestClass]
    public class OpenXmlUtilityTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        [TestInitialize]
        public void ClearPreviousTestResults()
        {
            var directory = Directory.GetParent(TestContext.TestDir);

            directory.GetFiles()
                .OrderByDescending(f => f.LastAccessTime).Skip(1)
                .ToList().ForEach(f => f.Delete());

            directory.GetDirectories()
                .OrderByDescending(d => d.LastAccessTime).Skip(1)
                .ToList().ForEach(d => d.Delete(true));
        }

        [TestMethod]
        public void ShouldGenerateDocx()
        {
            var outputPath = Test.WWSIOFOLDER + "GenerateOfficeDocument.docx";

            using(var package = WordprocessingDocument.Create(outputPath, WordprocessingDocumentType.Document))
            {
                OpenXmlUtility.GenerateOfficeDocument(package);
            }
        }

        [TestMethod]
        public void ShouldGenerateFlatFromOpc()
        {
            var pathToOpc = Test.WWSIOFOLDER + "FlatOpcToHtml.docx";
            var pathToFlat = Test.WWSIOFOLDER + "FlatOpcToHtml.xml";
            var flatDoc = OpenXmlUtility.TransformOpcToFlat(pathToOpc);
            flatDoc.Save(pathToFlat);
        }

        [TestMethod]
        public void ShouldGenerateHtmlFromByteArray()
        {
            var pathToTransformations = Test.WWSTRANSFORMSFOLDER;
            using(var catalog = CompositionUtility.GetDirectoryCatalog(pathToTransformations))
            {
                using(var container = CompositionUtility.GetCompositionContainer(catalog))
                {
                    OpenXmlUtility.ComposeTransformations(container);

                    var inputPath = Test.WWSIOFOLDER + "FlatOpcToHtml.docx";
                    var outputPath = Test.WWSIOFOLDER + "FlatOpcToHtml.html";

                    var settings = new SonghayHtmlConverterSettings()
                    {
                        PageTitle = "My Page Title " + DateTime.Now.ToString("U"),
                        UseEntityMap = false
                    };

                    OpenXmlUtility.WriteHtmlFile(inputPath, outputPath, settings);
                }
            }
        }

        [TestMethod]
        public void ShouldGenerateOpc()
        {
            var pathToFlatOpc = Test.TESTPROJECTFOLDER + "FlatOPC.xml";
            var flatOpcDoc = XDocument.Load(pathToFlatOpc);
            OpenXmlUtility.TransformFlatToOpc(flatOpcDoc, Environment.ExpandEnvironmentVariables("%UserProfile%/Desktop/OPCresult.docx"));
        }

        [TestMethod]
        public void ShouldGenerateOpcWithSdt()
        {
            var pathToFlatOpc = Test.TESTPROJECTFOLDER + "WordMLContentControlBug.xml";
            var flatOpcDoc = XDocument.Load(pathToFlatOpc);
            OpenXmlUtility.TransformFlatToOpc(flatOpcDoc, Environment.ExpandEnvironmentVariables("%UserProfile%/Desktop/OPCresult.docx"));
        }
    }
}
